package business.global.pk.alg.params;

import jsproto.c2s.cclass.pk.BasePocker;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhujianming
 * @date 2020-07-14 09:28
 * 算法抽象类型算法
 */
public class BasePKParameter {
    //剩余的手牌
    public int remainingCardSize = 17;
    //该玩家手上的所有牌
    public ArrayList<Integer> privateCardsList;
    //本次的手牌
    private ArrayList<Integer> opTypeCardsList;
    //本次的癞子替代牌
    public List<Integer> substituteCard;
    //上次的操作类型
    public int previousOpType;
    //上次的牌
    public ArrayList<Integer> previousCardList;
    //上次的对比值
    public int compareValue = 0;
    //上次的飞机长度或者顺子长度
    public int lastTripleNum = 0;

    //机器人部分
    public int weight;
    public Object robotInfo;

    //中间参数
    public int targetType;//目标类型
    public int laiziCard;//癞子牌
    private int laiziNum;

    /**
     * 获取克隆牌型
     *
     * @return 克隆牌
     */
    public ArrayList<Integer> getCloneOpTypeCardList() {
        return (ArrayList<Integer>) opTypeCardsList.clone();
    }
    public List<Integer> getCloneSubstituteCard() {
        if(substituteCard.isEmpty() || substituteCard.size()!=opTypeCardsList.size()){
            return new ArrayList<>(opTypeCardsList);
        }
        return new ArrayList<>(substituteCard);
    }
    public boolean verifySubstituteCard() {
        if(substituteCard.isEmpty() || substituteCard.size()!=opTypeCardsList.size()){
            return false;
        }
        List<Integer> noLaiList = opTypeCardsList.stream().filter(k -> !isLaiZi(k,laiziCard)).collect(Collectors.toList());
        int laiziNum = opTypeCardsList.size() - noLaiList.size();
        List<Integer> cloneOpTypeCardList = getCloneSubstituteCard();
        removeAll(cloneOpTypeCardList,noLaiList);
//        cloneOpTypeCardList.removeAll(noLaiList);
        return cloneOpTypeCardList.size() == laiziNum;
    }

    private void removeAll(List<Integer> cloneOpTypeCardList,List<Integer> noLaiList) {
        for (int i = 0; i < noLaiList.size(); i++) {
            for (int j = 0; j < cloneOpTypeCardList.size(); j++) {
                if(cloneOpTypeCardList.get(j)== noLaiList.get(i)){
                    cloneOpTypeCardList.remove(j);
                    break;
                }
            }
        }
    }

    public void resetLaiziNum() {
        List<Integer> noLaiList = opTypeCardsList.stream().filter(k -> !isLaiZi(k,laiziCard)).collect(Collectors.toList());
        laiziNum = opTypeCardsList.size() - noLaiList.size();
    }

    private boolean isLaiZi(int card, int laiziCard) {
        return getCardValue(card) == getCardValue(laiziCard);
    }

    public int getCardValue(int card) {
        return BasePocker.getCardValueEx(card);
    }
    public ArrayList<Integer> getClonePrivateCardsList() {
        return (ArrayList<Integer>) privateCardsList.clone();
    }

    /**
     * 获取手牌
     *
     * @return 手牌
     */
    public ArrayList<Integer> getOpTypeCardsList() {
        return opTypeCardsList;
    }

    /**
     * 初始化手牌，上回合操作牌
     *
     * @param previousCardList  上回合操作牌
     * @param previousOpType    上回合操作类型
     * @param opTypeCardsList   操作的牌，客户端操作的牌
     * @param remainingCardSize 剩余手牌张数
     */
    public void init(List<Integer> previousCardList, int previousOpType, int compareValue, int lastTripleNum, ArrayList<Integer> opTypeCardsList, List<Integer> substituteCard, int remainingCardSize) {
        this.remainingCardSize = remainingCardSize;
        this.previousCardList = (ArrayList<Integer>) previousCardList;
        this.previousOpType = previousOpType;
        this.opTypeCardsList = opTypeCardsList;
        this.substituteCard = substituteCard;
        this.compareValue = compareValue;
        this.lastTripleNum = lastTripleNum;
    }

}
